package com.dg.cuckoo.admin.service.impl;

import com.dg.cuckoo.admin.dto.basicInformation.constructionUnit.ConstructionUnitDto;
import com.dg.cuckoo.admin.dto.basicInformation.organization.OrganizationDto;
import com.dg.cuckoo.admin.dto.basicInformation.permissionSettings.UnitTypeDto;
import com.dg.cuckoo.admin.dto.basicInformation.userInfo.UserInfoDto;
import com.dg.cuckoo.admin.repository.ConstructionRepository;
import com.dg.cuckoo.admin.repository.OrganizationRepository;
import com.dg.cuckoo.admin.repository.UnitTypeRepository;
import com.dg.cuckoo.admin.repository.UserInfoRepository;
import com.dg.cuckoo.admin.service.ConstructionService;
import com.dg.cuckoo.admin.service.OrganizationService;
import com.dg.cuckoo.admin.service.ProjectInfoServer;
import com.dg.cuckoo.admin.vo.FreeVo;
import com.dg.cuckoo.admin.vo.UserInfoVo;
import com.dg.cuckoo.common.utils.ListUtil;
import com.dg.cuckoo.core.ServiceException;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrganizationServiceimpl implements OrganizationService {

    @Autowired
    private OrganizationRepository repository;
    @Autowired
    private ProjectInfoServer projectInfoServer;
    @Autowired
    private UnitTypeRepository unitTypeRepository;
    @Autowired
    private ConstructionRepository constructionRepository;
    @Autowired
    private UserInfoRepository userInfoRepository;
    @Autowired
    private ConstructionService constructionService;

    @Override
    public void save(String userid, List<String> units) {
        if(userid==null){
            throw new ServiceException("未选择用户");
        }
        List<OrganizationDto> dtos = repository.findOrganizationDtosByUserId(userid);
        if(dtos.size()>0){
            repository.deleteAll(dtos);
        }
        if(units.size()>0){
            for(String s:units){
                OrganizationDto dto = new OrganizationDto();
                dto.setUserId(userid);
                dto.setUnit(s);
                repository.save(dto);
            }
        }

    }

    @Override
    public String view() {
        JSONObject json = new JSONObject();
        List<UserInfoVo> freeVos=new ArrayList<>();
        String[] freeName=JSONObject.fromObject(projectInfoServer.clickUnitInfo()).getString("constructionName").replace("[","").replace("]","").replace("\"","").split(",");
        for(String s:freeName){
            List<String> list = constructionService.byUnitType(s);//组织结构二级标签
            List<FreeVo> freeVos1=new ArrayList<>();
            for(String unitName:list){
                List<OrganizationDto> byUnit = repository.findOrganizationDtosByUnit(unitName);
                List<UserInfoDto> userInfoDtos=new ArrayList<>();
                for(OrganizationDto o:byUnit){
                    userInfoDtos.add(userInfoRepository.findById(o.getUserId()).get());
                }
                freeVos1.add(new FreeVo(unitName,userInfoDtos));
            }

            List<OrganizationDto> byUnit = repository.findOrganizationDtosByUnit(s);
            List<UserInfoDto> userInfoDtos=new ArrayList<>();
            for(OrganizationDto o:byUnit){
                userInfoDtos.add(userInfoRepository.findById(o.getUserId()).get());
            }
            freeVos.add(new UserInfoVo(s,userInfoDtos,freeVos1));
        }


        List<OrganizationDto> all = repository.findAll();
        List<UserInfoDto> all1 = userInfoRepository.findAll();
        List<String> list=new ArrayList<>();
        List<String> list1=new ArrayList<>();
        for(OrganizationDto o:all){
            list.add(userInfoRepository.findById(o.getUserId()).get().getUserName());
        }
        for (UserInfoDto u:all1){
            list1.add(u.getUserName());
        }
        List<String> stringList = ListUtil.receiveDefectList(list, list1);//差集
        json.put("unit",projectInfoServer.clickUnitInfo());//组织架构单位里的信息
        json.put("date",freeVos);//用户名称树与数据
        json.put("list",stringList);//没有单位的用户
        return json.toString();
    }

    @Override
    public List<String> openUnitByUser(String userId) {
        List<OrganizationDto> dtos = repository.findOrganizationDtosByUserId(userId);
        List<String> list=new ArrayList<>();
        for (OrganizationDto o:dtos){
            list.add(o.getUnit());
        }
        return list;
    }

}
